package com.example.udpscreen;



import java.net.SocketException;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.example.udpscreen.events.CaptureEvent;
import com.example.udpscreen.events.RecvFileEvent;
import com.example.udpscreen.listeners.FinishListener;
import com.example.udpscreen.listeners.ProgressListener;
import com.example.udpscreen.runnables.MonitorRunnable;
import com.example.udpscreen.runnables.RecvResumeFileRunnable;
import com.example.udpscreen.servers.ADPlayerSocket;
import com.example.udpscreen.utils.FileUtil;

public class MainActivity extends Activity {

	private ADPlayerSocket  mAdSocket;
	
	private MyProgressBar mProgressBar;
	
	
	
	private RecvResumeFileRunnable mRecvFileRunnable=null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mProgressBar = (MyProgressBar) findViewById(R.id.pgsBar);
		FileUtil.initFilePath();
		EventBus.getDefault().register(this);
		runUdpServer();
	}
	
	/**
	 * 更新进度条
	 */
	Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				long send_size = (Long) msg.obj;
				if (send_size > mProgressBar.getMax()) {
					send_size = mProgressBar.getMax();
				}
				mProgressBar.setProgress((int) send_size);
				break;
			case 1:
				boolean success = (Boolean) msg.obj;
				Toast.makeText(MainActivity.this, "success:"+success, Toast.LENGTH_SHORT).show();
				break;
			case 2:
				mProgressBar.setMax(msg.arg1);
				break;
			default:
				break;
			}
		}
	};
	
	/**
	 * 进度条回调
	 */
	ProgressListener progressListener = new ProgressListener() {
		
		@Override
		public void progessChanged(long send_size, long total) {
			Log.d("progressListener", "send_size:"+send_size+" total:"+total);
			Message msg = handler.obtainMessage();
			msg.obj = send_size;
			msg.what = 0;
			handler.sendMessage(msg);
		}
	};
	/**
	 * 结束回调
	 */
	FinishListener finishListener = new FinishListener() {
		
		@Override
		public void finish(boolean success,String error) {
			if(success)
				Log.d("finishListener", "success:"+success);
			else
				Log.d("finishListener", error);
		}
	};
	
	
	private void runUdpServer(){
		try {
			mAdSocket=new ADPlayerSocket(12017, 1000*10);
		} catch (SocketException e) {
			e.printStackTrace();
		}
		mAdSocket.startWork();
	}
	
	@Subscribe(threadMode = ThreadMode.POSTING)
	public void onEvent(RecvFileEvent event){
		mRecvFileRunnable= new RecvResumeFileRunnable(mAdSocket,progressListener,finishListener);
		String file_name = event.FileName;
		String file_path = event.FilePath;
		String file_md5  = event.Md5;
		long file_size = event.FileSize;
		Message msg = handler.obtainMessage();
		msg.what = 2;
		msg.arg1 = (int) file_size;
		handler.sendMessage(msg);
		mRecvFileRunnable.updateData(file_path, file_name,file_size, file_md5,event.mRemoteAddress,event.mRemotePort);
		mAdSocket.setRecvResumeFileRunnable(mRecvFileRunnable);
		new Thread(mRecvFileRunnable).start();
		System.out.println("file:"+file_name+" begin recv !" );
	}
	
	@Subscribe(threadMode = ThreadMode.POSTING)
	public void onEvent(CaptureEvent event){
		new Thread(new MonitorRunnable(mAdSocket,event.mRemoteAddress,event.mRemotePort)).start();
	}
	
}
